package controller

import (
	"haiqiu-go-admin/result"
	"haiqiu-go-admin/service"
	"log"
	"net/http"
)

func (*LogApi) LogApiHandle(w http.ResponseWriter, r *http.Request) {
	switch r.Method {
	case http.MethodGet:
		LogAPI.loginLogById(w, r)
		break
	case http.MethodPost:
		LogAPI.loginLogAdd(w, r)
		break
	case http.MethodPut:
		LogAPI.loginLogUpdate(w, r)
		break
	case http.MethodDelete:
		LogAPI.loginLogDeleteBatch(w, r)
		break
	default:
		break
	}
}

func (*LogApi) Statistics(w http.ResponseWriter, r *http.Request) {
	mapData, err := service.StatisticsLog()
	if err != nil {
		log.Println(err.Error())
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, mapData)
}

func (*LogApi) LogPage(w http.ResponseWriter, r *http.Request) {
	page, err := service.LogService{}.LogPage(w, r)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, page)
}

// 根据ID查询数据
func (*LogApi) loginLogById(w http.ResponseWriter, r *http.Request) {
	page, err := service.LogService{}.LogById(w, r)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, page)
}

// 新增数据
func (*LogApi) loginLogAdd(w http.ResponseWriter, r *http.Request) {
	add, err := service.LogService{}.LogAdd(w, r)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, add)
}

// 批量删除数据
func (*LogApi) loginLogDeleteBatch(w http.ResponseWriter, r *http.Request) {
	add, err := service.LogService{}.LogDeleteBatch(w, r)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, add)
}

// 根据ID更新数据
func (*LogApi) loginLogUpdate(w http.ResponseWriter, r *http.Request) {
	add, err := service.LogService{}.LogUpdate(w, r)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, add)
}

// LogList 查询所有数据
func (*LogApi) LogList(w http.ResponseWriter, r *http.Request) {
	loginLogList, err := service.LogService{}.LogList(w, r)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, loginLogList)
}
